﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static CCS_Snake.Contracts;

namespace CCS_Snake
{
    public partial class FrmMain : Form
    {
        MapPaintStruct MAP_PAINT;
        Graphics MAP;

        SnakeColorStruct SNAKE_COLOR;
        
        Thread GAME_TIMER;
        object[] GAME_TIME_PARAMETER;

        Thread GAME_RUNNING;
        object[] GAME_RUNNING_PARAMETER;

        public FrmMain()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 重写窗体键盘监听事件
        /// </summary>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData == Keys.Left || keyData == Keys.Right || keyData == Keys.Up || keyData == Keys.Down)
            {
                return false;
            } else
            {
                return base.ProcessDialogKey(keyData);
            }
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            MAP_PAINT = Utils.MapPaint(36, 36, 10, ColorTranslator.FromHtml("#DBDBDB"), ColorTranslator.FromHtml("#F5F5F5"));
            MAP = PnlMap.CreateGraphics();

            SNAKE_COLOR = Utils.SnakeColor(
                ColorTranslator.FromHtml("#E63E31"),
                ColorTranslator.FromHtml("#101010"),
                ColorTranslator.FromHtml("#C5C5C5"),
                ColorTranslator.FromHtml("#F9C01C")
            );

            GAME_TIMER = new Thread(GameEvent.ThreadTimeCountDown);
            GAME_TIME_PARAMETER = new object[] { this, LblGameTime };

            GAME_RUNNING = new Thread(GameEvent.ThreadGameRunning);
            GAME_RUNNING_PARAMETER = new object[] { this, MAP_PAINT, MAP, SNAKE_COLOR, BtnGameOver, LblLength, LblScore };

            BtnGameControls.Width = 156;
            BtnGameControls.Text = "开始";

            BtnGameOver.Visible = false;
        }

        private void FrmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (GAME_TIMER != null && GAME_TIMER.IsAlive) GAME_TIMER.Abort();
            if (GAME_RUNNING != null && GAME_RUNNING.IsAlive) GAME_RUNNING.Abort();
        }

        private void FrmMain_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.W:
                case Keys.Up: // 方向键 - 上
                    Parameter.SnakeDirectionList.Add(Parameter.MoveDirection.ToUp);
                    break;
                case Keys.S:
                case Keys.Down: // 方向键 - 下
                    Parameter.SnakeDirectionList.Add(Parameter.MoveDirection.ToDown);
                    break;
                case Keys.A:
                case Keys.Left: // 方向键 - 左
                    Parameter.SnakeDirectionList.Add(Parameter.MoveDirection.ToLeft);
                    break;
                case Keys.D:
                case Keys.Right: // 方向键 - 右
                    Parameter.SnakeDirectionList.Add(Parameter.MoveDirection.ToRight);
                    break;
            }
        }

        #region 绘制地图
        private void PnlMap_Paint(object sender, PaintEventArgs e)
        {
            GameEvent.MapClear(MAP_PAINT, MAP);
        }
        #endregion

        private void BtnGameControls_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;

            Thread thread = null;
            object[] threadParameter;

            switch (button.Text)
            {
                case "恢复":
                case "开始":
                    if (button.Text == "开始")
                    {
                        // 清空地图
                        PnlMap_Paint(null, null);

                        // 随机生成蛇的移动方向
                        Parameter.SnakeDirection = Utils.RandomSnakeMoveDirection();

                        // 创建贪吃蛇
                        Parameter.SnakeBody = Utils.CreateSnakeBody(MAP_PAINT, Parameter.SnakeDirection);

                        // 绘制贪吃蛇
                        GameEvent.DrawSnake(MAP_PAINT, MAP, SNAKE_COLOR);

                        // 初始化食物
                        Parameter.Food = new Point(-1, -1);

                        // 初始化游戏分数
                        LblLength.Text = "0";
                    }
                    
                    // 修改按钮显示状态
                    button.Text = "暂停";

                    // 修改游戏状态
                    Parameter.GameState = "Running";

                    // 时间动效线程
                    if (GAME_TIMER == null || !GAME_TIMER.IsAlive)
                    {
                        threadParameter = new object[] { this, LblGameTime };
                        GAME_TIMER.Start(GAME_TIME_PARAMETER);
                    }

                    // 控件动效线程
                    if (thread == null || !thread.IsAlive)
                    {
                        thread = new Thread(LayoutEvent.ThreadBtnGameControlsWidth);
                        threadParameter = new object[] { this, button, button.Width, Parameter.BtnGameControlsLayout.minWidth, BtnGameOver, true };
                        thread.Start(threadParameter);
                    }

                    // 游戏线程
                    if (GAME_RUNNING == null || !GAME_RUNNING.IsAlive) GAME_RUNNING.Start(GAME_RUNNING_PARAMETER);
                    break;
                case "暂停":
                    button.Text = "恢复";

                    Parameter.GameState = "Paused";

                    // 控件动效线程
                    if (thread == null || !thread.IsAlive)
                    {
                        thread = new Thread(LayoutEvent.ThreadBtnGameControlsWidth);
                        threadParameter = new object[] { this, button, button.Width, Parameter.BtnGameControlsLayout.maxWidth, BtnGameOver, false };
                        thread.Start(threadParameter);
                    }
                    break;
                default:
                    break;
            }
        }

        private void BtnGameOver_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;

            BtnGameControls.Text = "开始";

            LblGameTime.Text = Utils.IntToTime(0);

            Parameter.GameState = "Stoped";

            MessageBox.Show("游戏结束", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);

            // 控件动效线程
            Thread thread = null;
            if (thread == null || !thread.IsAlive)
            {
                thread = new Thread(LayoutEvent.ThreadBtnGameControlsWidth);
                object[] threadParameter = new object[] { this, BtnGameControls, BtnGameControls.Width, Parameter.BtnGameControlsLayout.maxWidth, button, false };
                thread.Start(threadParameter);
            }

        }

        private void TsmiReward_Click(object sender, EventArgs e)
        {
            FrmReward frmReward = new FrmReward();
            frmReward.ShowDialog();
        }
    }
}
